<template>
  <view class="filter-wrap">
    <view class="filter">
      <view v-for="(item, index) in options" :key="index">
        <template v-if="item.name === 'price'">
          <view class="price-item fliter-date">
            <view class="fliter-title">
              <text>租金预算</text>
              <text class="range-value">{{ rangeValue[0] }}-{{ rangeValue[1] === 10000 ? '不限' : rangeValue[1] + '元' }} </text>
            </view>
            <range-slider
              :value="rangeValue"
              :min="slideMin"
              :max="slideMax"
              :step="slideStep"
              :bar-height="barHeight"
              :block-size="blockSize"
              :background-color="backgroundColor"
              :tipVisible="false"
              @change="handleRangeChange"
            />
          </view>
          <view class="filter-wrap-item">
            <view
              :class="['filter-item', item.value.includes(items) ? 'active-item' : 'filter-item']"
              v-for="(items, indexs) in item.list"
              :key="indexs"
              @click="handleSelectPrice(item, items)"
            >
              {{ items }}
            </view>
          </view>
        </template>
        <template v-if="item.name !== 'price'">
          <view class="filter-title">
            {{ item.title }}
          </view>
          <view class="filter-wrap-item">
            <view
              :class="['filter-item', item.value.includes(items.value) ? 'active-item' : 'filter-item']"
              v-for="(items, indexs) in item.list"
              :key="indexs"
              @click="handleSelect(item, items)"
            >
              {{ items.title }}
            </view>
          </view>
        </template>
      </view>
    </view>
    <view class="fixed-button">
      <view class="reset" @click="reset">重置</view>
      <view class="confirm" @click="handleSubmit">确定</view>
    </view>
  </view>
</template>

<script>
import rangeSlider from '@/components/range-slider'
import { mapActions, mapGetters } from 'vuex'

export default {
  name: 'componentName',
  components: {
    rangeSlider,
  },
  mixins: [],
  props: {
    options: {
      type: Array,
      default: () => [],
    },
    propsValue: {
      type: Array,
      default: () => [],
    },
  },
  data() {
    return {
      rangeValue: [0, 10000],
      slideMin: 0,
      slideMax: 10000,
      slideStep: 100,
      barHeight: 8,
      blockSize: 24,
      userCountNumber: 222,
      backgroundColor: '#F0F0F0',
    }
  },
  // 侦听属性
  watch: {},
  // 计算属性
  computed: {
    ...mapGetters([]),
  },
  // 监听页面加载，该钩子被调用时data,computed,function,watch,props,slots已设置完成
  onLoad(options) {},
  // 监听页面显示，页面每次出现在屏幕上都触发
  onShow() {},
  // 监听页面初次渲染完成，此时组件已挂载完成
  onReady() {},
  // 下拉刷新
  onPullDownRefresh() {},
  // 加载更多
  onReachBottom() {},

  created() {
    this.options.forEach((v, i) => {
      this.propsValue.forEach((v1) => {
        if (v.name == v1.name) {
          if (v1.name == 'price') {
            const price = v1.value.match(/\d+/g).map((v) => {
              return Number(v)
            })
            this.rangeValue = price.length === 1 ? [...price, 10000] : price
          } else {
            v.value = v1.value
          }
          return
        }
      })
    })
  },
  mounted() {},
  destroyed() {},
  methods: {
    ...mapActions([]),
    // 选中或者取消选中
    handleSelect(item, { value }) {
      if (item.value.includes(value)) {
        item.value.forEach((v, i) => {
          if (v == value) {
            item.value.splice(i, 1)
          }
        })
      } else {
        item.value.push(value)
      }
      this.$forceUpdate()
    },
    handleSelectPrice(item, items) {
      console.log('handleSelectPrice', item, items)
      const list = items.match(/\d+/g).map((v) => {
        return Number(v)
      })
      item.value = items
      if (list.length && list.length > 1) {
        this.rangeValue = list
      } else {
        this.rangeValue = [list[0], 10000]
      }
      this.$forceUpdate()
    },
    reset() {
      this.options.forEach((v, i) => {
        v.value = []
      })
      this.$emit('emit-filterData', [])
    },
    handleSubmit() {
      const filter = this.options.filter((v) => {
        const map = {
          key: v.name,
          value: v.value,
        }
        if (v.value.length > 0) {
          return map
        }
      })
      console.log('filter', filter)
      this.$emit('emit-filterData', filter)
    },
    handleRangeChange(e) {
      this.rangeValue = e
      // console.log(e)
      this.options.forEach((v, i) => {
        if (v.name == 'price') {
          v.value = `${e[0] + '-' + e[1]}`
        }
      })

      // console.log(this)
    },
  },
}
</script>

<style lang="scss" scoped>
.filter-wrap {
  // height: 100%;
  // height: 60vh;
  height: 1000rpx;
  .filter {
    box-sizing: border-box;
    width: 100vw;
    padding-bottom: 100px;
    overflow-y: scroll;
    // width: calc(100vw - 64rpx);
    height: 100%;
    padding: 16rpx 32rpx;
    background: white;
    .filter-title {
      font-weight: 600;
      font-size: 28rpx;
      color: #222222;
      margin-bottom: 24rpx;
    }
    .filter-wrap-item {
      display: flex;
      flex-wrap: wrap;
      margin-bottom: 40rpx;
      .filter-item {
        width: calc(25% - 16rpx);
        background: #f5f5f5;
        padding: 12rpx 0;
        text-align: center;
        font-weight: 400;
        font-size: 28rpx;
        color: #666666;
        margin-right: 16rpx;
        margin-bottom: 16rpx;
        border-radius: 16rpx;
      }
      .active-item {
        font-weight: 600;
        color: $uv-primary;
        background: #ecf5f8;
      }
    }
    .price-item {
      margin-bottom: 20rpx;
      .fliter-title {
        display: flex;
        font-weight: 600;
        font-size: 28rpx;
        color: #222222;
        margin-bottom: 24rpx;
        .range-value {
          margin-left: auto;
          font-weight: bold;
          font-size: 16px;
          color: $uv-primary;
        }
      }
    }
  }
  .fixed-button {
    border: 2rpx solid #f1f1f1;
    position: absolute;
    padding: 32rpx;
    width: 100%;
    bottom: 0;
    box-sizing: border-box;
    background: white;
    display: flex;
    justify-content: space-between;
    .reset {
      padding: 22rpx 52rpx;
      background: #f5f5f5;
      border-radius: 40rpx;
      color: #666666;
    }
    .confirm {
      background: $uv-primary;
      color: white;
      border-radius: 40rpx;
      padding: 22rpx 226rpx;
    }
  }
}
</style>
